﻿/**
 * 
 * Copyright 2009 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using mooseirc.irc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace mooseirc.Commands {

    /// <summary>
    /// Connect Command
    /// </summary>
    /// <remarks>
    /// Arguments expected as host port, or host:port or host,port
    /// </remarks>
    [Command(Name = "connect")]
    [Trigger(Trigger="server")]
    [Trigger(Trigger = "connect")]
    public sealed class ConnectCommand : IrcCommand {

        /// <summary>
        /// Initialize IrcConnect Command
        /// </summary>
        public ConnectCommand()
            : base() {
            return;
        }

        /// <summary>
        /// Return Valid States for this command to execute
        /// </summary>
        /// <returns>DisConnected State, no other is technically valid</returns>
        protected override IrcState validStates() {
            return IrcState.Disconnected;
        }

        /// <summary>
        /// Connect Action
        /// </summary>
        protected override void action(IrcConnection connection, IrcState state, string args, Stream outputStream)
        {
            byte[] buffer = Encoding.UTF8.GetBytes("Connecting to " + args);
            outputStream.Write(buffer, 0, buffer.Length);

            string[] strArgs = args.Split(new string[] { " ", ":", ","}, StringSplitOptions.RemoveEmptyEntries);

            //TODO: Add Error Trapping here, validate number and host prior to attempting to connnect

            connection.connect(strArgs[0].Trim(), int.Parse(strArgs[1].Trim()));
            return;
        }
    }
}
